<%
 
'**************************************************
'函数名： card_make
'作  用： 批量制卡
'参  数： 
'				batch ---- 批次名称(字符)
'				batch_num ---- 该批次卡数量(字符)
'				Conn ---- 数据库连接(对象)
'返回值： -1失败/XXXXXX批次编码(10位)(字符)
'注：GetRndPassword()需要引用 Common.All.asp
'**************************************************
Function Card_Make(batch,batch_num,Conn)
		batch=ReplaceBadChar(Trim(batch)) 
		batch_num=PE_CLng(ReplaceBadChar(Trim(batch_num)) )
		batchcode=GetRndPassword(10)&""
		''GetRndNum(6)卡验证码 6位
		''GetCardIdNum2String()20位 卡号 090812+13:45:20+XXXX+KKKKK(校验码)+N(版本)
		
    Dim strSql, RowCount, RowCountTotal, RowCount_Total_checkdb,cardcode
    RowCount = 0
    RowCountTotal=0
    If (batch <> "" and batch_num <> "" ) Then 
    dim xx
    xx=1
    	do while( xx <= batch_num)
    		cardcode=GetCardIdNum2String()
    		strSql = "select count(*)as 'aaaaa' from KH_XX where cardcode='"&cardcode&"'  "
			if (0= Conn.Execute(strSql)(0)) then
		    	strSql=" INSERT INTO KH_XX "
		      	strSql=strSql & " (id , cardcode, yzm, zt, batch, remarks, batchcode)"
		      	strSql=strSql & " select 1+ISNULL(MAX(id), 0) as 'cardid','"&cardcode&" ', '"&GetRndNum(6)&"', 0, '"&batch&"', '批量制卡', '"&batchcode&"' from KH_XX "
		    	Conn.Execute (strSql), RowCount    
		    	RowCountTotal=RowCountTotal+   RowCount  
		    	xx=xx+1
		    End If
     	loop
    End If
    
		strSql = "select count(cardcode)as 'aaaaa' from KH_XX where batchcode='"&batchcode&"'  "
		RowCount_Total_checkdb = Conn.Execute(strSql)(0)

    If (RowCountTotal = batch_num and RowCount_Total_checkdb=RowCountTotal) Then
        Card_Make=batchcode
    Else
        Card_Make="-1"
    End If

End Function

'**************************************************
'函数名：GetNumString
'作  用：获得随即数卡号
'返回值：随机无重复的数字(用于生成卡号)
''GetCardIdNum2String()20位 卡号 090812+13:45:20+XXXX+KKKKK(校验码)+N(版本)
'**************************************************
Function GetCardIdNum2String()
    Dim vcard_ymd, vcard_hms, vcard_mmm, checkcode,Card_Code_Temp,Card_Code_Temp1,Card_Code_Temp2
    vcard_ymd = Right("0" & Year(Now), 2) & Right("0" & Month(Now), 2) & Right("0" & Day(Now), 2)
    vcard_hms = Right("0" & Hour(Now), 2) & Right("0" & Minute(Now), 2) & Right("0" & Second(Now), 2)
    Randomize
    vcard_mmm = GetRndNum(4)''随机数位
    Card_Code_Temp = vcard_ymd & vcard_hms & vcard_mmm
    Card_Code_Temp1=""
    Card_Code_Temp2=""


		dim zs_array''映射数组
		zs_array= Array(Array(5,7,6,8,1,2,9,3,0,4),Array(9,6,1,4,5,7,2,8,3,0))

    For I=1 To len(Card_Code_Temp)
			Card_Code_Temp1=Card_Code_Temp1&""& zs_array(0)( Cint( mid(Card_Code_Temp,i,1)))
			Card_Code_Temp2=Card_Code_Temp2&""& zs_array(1)( Cint( mid(Card_Code_Temp,i,1)))
		Next
    
    ''checkcode=GetRndNum(3)''"123" 校验码
    checkcode=GetCardCheckCodeStr(Card_Code_Temp)& GetCardCheckCodeStr(Card_Code_Temp1)&GetCardCheckCodeStr(Card_Code_Temp2)''"123" 3位校验码
    GetCardIdNum2String = Card_Code_Temp & checkcode & "1" 
    ''GetCardIdNum2String = vcard_ymd & "-" & vcard_hms & "-" & vcard_mmm & "-" & checkcode & "-" & "0" 
End Function


'**************************************************
'函数名： GetCardCheckCodeStr
'作  用：将卡号前16位数字及其映射代码，根据下述公式进行计算，返回对应的校验码
'返回值：一位数字的校验码
''计算公式：
''设第n位卡号数字为E(n)，那么计算公式如下：
''X1=E(2)+E(4)+E(6)+E(8)+E(10)+E(12)+E(14)+E(16)
''U1=3*X1
''Y1=E(1)+E(3)+E(5)+E(7)+E(9)+E(11)+E(13)+E(15)
''Z1=U1+Y1
''H1=Z1的个位数
''K1=(10-H1)的个位数
'**************************************************
Function GetCardCheckCodeStr(str_kahao)
 	dim K1
	K1=(10- (Cint( mid(str_kahao,1,1))+Cint( mid(str_kahao,3,1))+Cint( mid(str_kahao,5,1))+Cint( mid(str_kahao,7,1))+Cint( mid(str_kahao,9,1))+Cint( mid(str_kahao,11,1))+Cint( mid(str_kahao,13,1))+Cint( mid(str_kahao,15,1))+3*(Cint( mid(str_kahao,2,1))+Cint( mid(str_kahao,4,1))+Cint( mid(str_kahao,6,1))+Cint( mid(str_kahao,8,1))+Cint( mid(str_kahao,10,1))+Cint( mid(str_kahao,12,1))+Cint( mid(str_kahao,14,1))+Cint( mid(str_kahao,16,1)))) mod 10) mod 10
  GetCardCheckCodeStr =  K1&""
End Function



%>
		



   